package com.shop.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.shop.dao.GoodsOrderRemarkMapper;
import com.shop.pojo.GoodsOrderRemark;
import com.shop.pojo.User;
import com.shop.service.GoodsOrderRemarkService;
import com.shop.service.LoginService;
import com.shop.util.BaseUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class GoodsOrderRemarkServiceImpl implements GoodsOrderRemarkService {

    @Autowired
    private GoodsOrderRemarkMapper goodsOrderRemarkMapper;

    @Autowired
    private LoginService loginService;

    /*多条件查询商品评论*/
    @Override
    public List<GoodsOrderRemark> queryGoodsRemarkByCondition(Map<String, Object> params) throws Exception {
        return goodsOrderRemarkMapper.queryGoodsRemarkByCondition(params);
    }

    /*添加商品评论或追加评论*/
    @Override
    public String addGoodsRemark(HttpServletRequest request, HttpServletResponse response) throws Exception {
        String data =  request.getParameter("data");
        String token =  request.getParameter("token");
        User user = loginService.getUserByToken(token);     //验证用户信息是否已过期
        if(null == user){
            return "添加失败：用户已失效，请重新登录后再试";
        }

        String dataJson = BaseUtil.toJson(data);    //把数据转化为json字符串
        GoodsOrderRemark goodsOrderRemark = JSONObject.parseObject(dataJson,GoodsOrderRemark.class);
        if(StringUtils.isEmpty(goodsOrderRemark.getOrderId())){
            return "添加失败：用户未购买该商品，请购买后再试";
        }

        //第一次评论，直接生成ID
        if(StringUtils.isEmpty(goodsOrderRemark.getId())){
            goodsOrderRemark.setId(BaseUtil.getSID());

        }else if(StringUtils.isNotEmpty(goodsOrderRemark.getId()) && (StringUtils.isEmpty(goodsOrderRemark.getFirstid()))){
            //已有评论Id但没有追加Id，则属于追加评论，把第一次评论的评分和内容拿过来
            GoodsOrderRemark goodsOrderRemarkSuper = goodsOrderRemarkMapper.selectByPrimaryKey(goodsOrderRemark.getId());

            goodsOrderRemark.setGoodsRemark(goodsOrderRemarkSuper.getGoodsRemark());
            goodsOrderRemark.setGoodsRemarkImg(goodsOrderRemarkSuper.getGoodsRemarkImg());
            goodsOrderRemark.setFirstid(goodsOrderRemarkSuper.getId());
            goodsOrderRemark.setId(BaseUtil.getSID());
        }

        //设置用户ID、创建时间等
        goodsOrderRemark.setUserId(user.getUserId());
        goodsOrderRemark.setCreateTime(new Date());
        goodsOrderRemarkMapper.insert(goodsOrderRemark);

        return "success";
    }

    /*删除评论*/
    @Override
    public String deleteGoodsRemark(HttpServletRequest request, HttpServletResponse response) throws Exception {
        String remarkId = request.getParameter("remarkId"); //获取评论ID，根据评论ID去删除
        if(StringUtils.isNotEmpty(remarkId)){
            goodsOrderRemarkMapper.deleteByPrimaryKey(remarkId);
            return "success";
        }else{
            return "删除失败：该评论不存在，请刷新页面后重试";
        }

    }


}
